package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.BusinessListAdapter;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;
import com.j256.ormlite.dao.Dao;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class UserCollectActivity extends BaseActivity {

	/****************** 视图变量 **************************/

	private ListView topListView;
	private TextView tipTv;
	/****************** 其它变量 **************************/
	private List<BusinessEntity> businessTopList;
	private BusinessListAdapter businessListAdapter;
	private String userId;
	private OzBlipApplication myApp;
	private Dao<BusinessEntity, Integer> businessDao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_collect);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 获取列表数据
		requestTopListData();

	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.topListView = (ListView) findViewById(R.id.lv_top_businessList);
		this.tipTv = (TextView) findViewById(R.id.tip_text_noData);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {

		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent settingIntent = new Intent(UserCollectActivity.this,
							SettingActivity.class);
					startActivity(settingIntent);
					break;

				default:
					break;
				}
			}
		};
		this.topListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> itemView, View arg1,
					int parameInt, long arg3) {
				Intent businessIntent = new Intent(UserCollectActivity.this,
						BusinessInfoActivity.class);
				BusinessEntity business = UserCollectActivity.this.businessTopList
						.get(parameInt);
				businessIntent.putExtra("businessInfo", business);
				startActivity(businessIntent);
			}
		});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		 Intent intent = getIntent();
		 userId = intent.getStringExtra("userId");
	}

	/**
	 * 获取评论列表数据
	 */
	private void requestTopListData() {

		// myApp = (OzBlipApplication) getApplication();
		// try {
		// businessDao = myApp.getDBHelper().getDao(BusinessEntity.class);
		// businessTopList = businessDao.queryForAll();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		//
		// setTopListAdapter();
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
						nearbyBusinessParamMap.put("userId",userId);
						nearbyBusinessParamMap.put("pageCnt", "1");
						nearbyBusinessParamMap.put("pageRow", "20");
						nearbyBusinessParamMap.put("language", "1");
						// 发送请求
						byte[] commentItemList = HttpUtil.requestByPost(
								UserCollectActivity.this,
								Constants.USER_COLLECT_LIST_API,
								nearbyBusinessParamMap);
						if (commentItemList != null) {
							businessTopList = new ParseJson()
									.parseUserCollectListJson(commentItemList);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setTopListAdapter();
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用用户收藏列表适配器
	 */
	public void setTopListAdapter() {
		if (CollectionUtil.UnNullAndZero(businessTopList)) {
			this.tipTv.setVisibility(8);
			this.businessListAdapter = new BusinessListAdapter(
					UserCollectActivity.this);
			this.businessListAdapter.addItems(businessTopList);
			this.topListView.setAdapter(this.businessListAdapter);
		} else {
			this.tipTv.setVisibility(0);
			this.tipTv.setText(getResources().getString(R.string.no_collect));
		}
	}
}